package Model;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import java.util.Vector;

import javax.swing.table.AbstractTableModel;

import Dao.SearchDaoHelper;
import Entity.Transfer;

public class SearchModel extends AbstractTableModel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String[] header = { "EmpNumber", "EmpName", "FromLocation",
			"ToLocation", "FromProject", "ToProject", "From Dept", "ToDept",
			"Allowance" };
	private SearchDaoHelper dao = new SearchDaoHelper();
	private List<Transfer> transferList = new Vector<Transfer>();
	@Override
	public int getColumnCount() {
		// TODO Auto-generated method stub
		return header.length;
	}

	@Override
	public int getRowCount() {
		// TODO Auto-generated method stub
		return transferList.isEmpty() == true ? 0 : transferList.size();
	}

	@Override
	public String getColumnName(int column) {
		// TODO Auto-generated method stub
		return header[column];
	}

	public List<Transfer> getTransferList() {
		return transferList;
	}

	@Override
	public Object getValueAt(int row, int col) {
		// TODO Auto-generated method stub
		if (transferList.isEmpty())
			return null;
		Transfer item = transferList.get(row);
		switch (col) {
		case 0:
			return item.getEmpNumber();
		case 1:
			return item.getEmpName();
		case 2:
			return item.getFromLocation();
		case 3:
			return item.getToLocation();
		case 4:
			return item.getFromProject();
		case 5:
			return item.getToProject();
		case 6:
			return item.getFromDepartment();
		case 7:
			return item.getToDepartment();
		case 8:
			return item.getAllowance();
		default:
			return null;
		}
	}

	public void searchTransfer(String empNumber, String empName,
			String fromLocation, String toLocation, String fromProject,
			String toProject, String fromDept, String toDept)
			throws IOException, ClassNotFoundException, SQLException {
		transferList = dao.searchTransfer(empNumber, empName, fromLocation,
				toLocation, fromProject, toProject, fromDept, toDept);
		fireTableDataChanged();
	}

	public String getNameByID(String string) throws SQLException,
			ClassNotFoundException, IOException {
		String result = dao.getName(string);
		return result;
	}
}
